package com.googlecode.likesql.expression;

import java.util.Map;
import java.util.TreeMap;

/**
 * AtExp represents a precompiled expression with pattern and designated parameters.
 * String starts with @ in the pattern is regarded as a parameter. 
 * The value which is setted to the parameter is converted by a configured {@link com.googlecode.likesql.converter.Converter Converter} according to its class.
 * 
 * 
 * @author Johny Huang
 * @since 0.1
 * @see {@link com.googlecode.likesql.converter.AtExpConverter AtExpConverter}
 */
public class AtExp {

	/**
	 * Pattern for expression
	 */
	private String pattern;
	
	/**
	 * Map for parameter/value
	 */
	private Map values;
	
	private AtExp(String pattern){
		this.pattern=pattern;
		values=new TreeMap();
	}
	
	/**
	 * Gets a new instance with a pattern.
	 * @param pattern
	 * @return
	 */
	public static AtExp at(String pattern){
		return new AtExp(pattern);
	}
	
	/**
	 * Sets the value to the parameter.{@link IllegalArgumentException} is thrown if parameter is null.
	 * 
	 * @param param parameter
	 * @param value
	 * @return
	 */
	public AtExp set(String param,Object value){
		if(param==null){
			throw new IllegalArgumentException("param can't be null!");
		}
		values.put(param, value);
		return this;
	}
	
	/**
	 * Sets the value to the parameter.
	 * 
	 * @param param
	 * @param value
	 * @return
	 * @see #set(String, Object)
	 */
	public AtExp set(String param,long value){
		return set(param,new Long(value));
	}
	
	/**
	 * Sets the value to the parameter.
	 * 
	 * @param param
	 * @param value
	 * @return
	 * @see #set(String, Object)
	 */
	public AtExp set(String param,double value){
		return set(param,new Double(value));
	}
	
	/**
	 * Sets the value to the parameter.
	 * 
	 * @param param
	 * @param value
	 * @return
	 * @see #set(String, Object)
	 */
	public AtExp set(String param,char value){
		return set(param,String.valueOf(value));
	}

	/**
	 * Gets the pattern.
	 * 
	 * @return
	 */
	public String getPattern() {
		return pattern;
	}

	/**
	 * Get the parameters/values as map.
	 * @return
	 */
	public Map getValues() {
		return values;
	}
}
